import {onMounted, ref, onBeforeUnmount,watch} from 'vue'
//浏览器始终保持100%的宽度，高度随提供的宽高比（wh_ratio）适配
export default function player_resize(player, wh_ratio) {
    // player 是播放器ref对象
    // wh_ratio是宽高比例(宽/高)，可以利用该功能让播放器总是和视频保持相同比例
    const player_ref = ref(null)
    const player_resize = () => {
        let parentWidth = player_ref.value.offsetWidth;
        let height = parentWidth / wh_ratio.value;
        // console.log(parentWidth + "px", height + "px")
        player.value.setPlayerSize("100%", height + "px");
    }
    onMounted(() => {
        window.addEventListener("resize", player_resize);
        player_resize()
    })
    onBeforeUnmount(() => {
        window.removeEventListener("resize", player_resize); //移除窗口响应事件
        // console.log('removeEventListener')
    })
    watch(wh_ratio,player_resize)
    return {
        player_ref
    }

}